package com.tsj.spzx.controller;

import com.github.pagehelper.PageInfo;
import com.tsj.spzx.model.dto.system.AssginRoleDto;
import com.tsj.spzx.model.dto.system.SysUserDto;
import com.tsj.spzx.model.entity.system.SysUser;
import com.tsj.spzx.model.vo.common.Result;
import com.tsj.spzx.model.vo.common.ResultCodeEnum;
import com.tsj.spzx.service.SysUserService;
import org.junit.jupiter.api.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*", originPatterns = "*")
@Tag("用户信息接口")
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户信息 条件分页查询
     * @param pageNum  当前页码
     * @param pageSize  每页条数
     * @param sysUserDto 封装的前端传来的查询条件
     * @return
     */
    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<SysUser> findByPage(@PathVariable("pageNum") Integer pageNum,
                                      @PathVariable("pageSize") Integer pageSize,
                                      SysUserDto sysUserDto ){
        PageInfo<SysUser> pageInfo = sysUserService.finByPage(pageNum, pageSize, sysUserDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加用户
     * @param sysUser  封装前端传来的信息
     * @return
     */
    @PostMapping("/saveSysUser")
    public Result<SysUser> saveSysUser (@RequestBody SysUser sysUser){
        sysUserService.saveSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    /**
     * 修改用户信息
     * @param sysUser
     * @return
     */
    @PutMapping("/updateSysUser")
    public Result<SysUser> updateSysUser (@RequestBody SysUser sysUser){
        sysUserService.updateSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/deleteById/{userId}")
    public Result<SysUser> deleteSysUser (@PathVariable("userId") Integer userId){
        sysUserService.deleteSysUser(userId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    /**
     * 为用户分配角色
     * @param assginRoleDto 封装前端传来的数据  用户id 和 角色id 列表
     * @return
     */
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto){
        sysUserService.doAssign(assginRoleDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
